import { BasicColumn, FormSchema } from '/@/components/Table';
import { h } from 'vue';
import { Tag } from 'ant-design-vue';
import Icon from '@/components/Icon/Icon.vue';

export const columns: BasicColumn[] = [
  {
    title: 'ID',
    dataIndex: 'id',
    width: 50,
    align: 'left',
  },
  {
    title: '按钮名称',
    dataIndex: 'menu_name',
    width: 200,
  },
  {
    title: '图标',
    dataIndex: 'icon',
    width: 50,
    customRender: ({ record }) => {
      return h(Icon, { icon: record.icon });
    },
  },
  {
    title: '接口路径',
    dataIndex: 'api_url',
    width: 200,
    customRender: ({ record }) => {
      return record.methods ? '[' + record.methods + ']  ' + record.api_url : record.api_url;
    },
  },
  {
    title: '前端权限',
    dataIndex: 'unique_auth',
    width: 300,
  },
  {
    title: '页面路由',
    dataIndex: 'menu_path',
    width: 240,
  },
  {
    title: '排序',
    dataIndex: 'sort',
    width: 50,
  },
  {
    title: '规则状态',
    dataIndex: 'is_show',
    width: 80,
    customRender: ({ record }) => {
      const status = record.is_show;
      const enable = ~~status === 0;
      const color = enable ? 'red' : 'green';
      const text = enable ? '停用' : '启用';
      return h(Tag, { color: color }, () => text);
    },
  },
];

// const isDir = (auth_type: string) => auth_type === '0';
const isMenu = (auth_type: number) => auth_type === 1;
const isButton = (auth_type: number) => auth_type === 2;

export const searchFormSchema: FormSchema[] = [
  {
    field: 'keyword',
    label: '按钮名称',
    component: 'Input',
    colProps: { span: 8 },
  },
  {
    field: 'is_show',
    label: '规则状态',
    component: 'Select',
    componentProps: {
      options: [
        { label: '停用', value: '0' },
        { label: '启用', value: '1' },
      ],
    },
    colProps: { span: 8 },
  },
];

export const formSchema: FormSchema[] = [
  {
    field: 'id',
    label: 'id',
    component: 'Input',
    defaultValue: 0,
    show: false,
  },
  {
    field: 'auth_type',
    label: '菜单类型',
    component: 'RadioButtonGroup',
    defaultValue: 1,
    componentProps: {
      options: [
        { label: '菜单', value: 1 },
        { label: '接口', value: 2 },
      ],
    },
    colProps: { lg: 24, md: 24 },
  },
  {
    field: 'menu_name',
    label: '菜单名称',
    component: 'Input',
    required: true,
  },
  {
    field: 'pid',
    label: '上级菜单',
    component: 'TreeSelect',
    componentProps: {
      fieldNames: {
        label: 'menu_name',
        key: 'id',
        value: 'id',
      },
      getPopupContainer: () => document.body,
    },
  },
  {
    field: 'menu_path',
    label: '路由地址',
    component: 'Input',
    required: true,
    ifShow: ({ values }) => isMenu(values.auth_type),
  },
  {
    field: 'unique_auth',
    label: '权限标识',
    component: 'Input',
    required: true,
    ifShow: ({ values }) => isMenu(values.auth_type),
  },
  {
    field: 'icon',
    label: '图标',
    component: 'IconPicker',
    defaultValue: '',
    ifShow: ({ values }) => isMenu(values.auth_type),
  },
  {
    field: 'methods',
    label: '请求方式',
    component: 'Select',
    defaultValue: '',
    componentProps: {
      options: [
        { label: '请选择请求方式', value: '' },
        { label: 'GET', value: 'GET' },
        { label: 'POST', value: 'POST' },
        { label: 'PUT', value: 'PUT' },
        { label: 'DELETE', value: 'DELETE' },
      ],
    },
    ifShow: ({ values }) => isButton(values.auth_type),
  },
  {
    field: 'api_url',
    label: '接口地址',
    component: 'Input',
    ifShow: ({ values }) => isButton(values.auth_type),
  },
  {
    field: 'sort',
    label: '排序',
    component: 'InputNumber',
    defaultValue: 0,
  },
  {
    field: 'is_show',
    label: '状态',
    component: 'RadioButtonGroup',
    defaultValue: 1,
    componentProps: {
      options: [
        { label: '禁用', value: 0 },
        { label: '启用', value: 1 },
      ],
    },
  },
  {
    field: 'is_show_path',
    label: '是否为隐藏菜单',
    component: 'RadioButtonGroup',
    defaultValue: 0,
    componentProps: {
      options: [
        { label: '否', value: 0 },
        { label: '是', value: 1 },
      ],
    },
    ifShow: ({ values }) => isMenu(values.auth_type),
  },
];
